package com.yookey.common.response;

import com.yookey.common.constant.ResponseMessageEnum;
import com.yookey.common.utils.ResponseMessageUtil;
import lombok.Data;

/**
 * className R
 *
 * @Author: Zhang Shao Hui
 * @Description:
 * @Date:Create：in 2019/7/8 13:45
 * @Modified By：
 */
@Data
public class R<T> {
    private int status; // 0:成功 1：失败
    private String code; // 编码
    private String message; // 消息内容
    private T data; // 数据
    public static R<?> ok() {
        R<?> result = new R<>();
        result.setCode(ResponseMessageEnum.SUCCESS.getMessageKey());
        result.setMessage("OK");
        result.setStatus(0);
        return result;
    }

    public static <T> R<T> ok(T modelData) {
        R<T> result = new R<T>();
        result.setCode(ResponseMessageEnum.SUCCESS.getMessageKey());
        result.setMessage("ok");
        result.setStatus(0);
        result.setData(modelData);
        return result;
    }

    public static <T> R<T> ok(T modelData, String message) {
        R<T> result = new R<T>();
        result.setCode(ResponseMessageEnum.SUCCESS.getMessageKey());
        result.setMessage(message);
        result.setStatus(0);
        result.setData(modelData);
        return result;
    }

//    public static R ok(String message) {
//        R result = new R<>();
//        result.setCode(ResponseMessageEnum.SUCCESS.getMessageKey());
//        result.setMessage(message);
//        result.setStatus(0);
//        return result;
//    }


    public static <T> R<T> fail(String message) {
        R<T> result = new R<>();
        result.setMessage(message);
        result.setCode(ResponseMessageEnum.BUSINESS_ERROR.getMessageKey());
        result.setStatus(1);
        return result;
    }

    public static R<?> fail(ResponseMessageEnum responseMessageEnum) {
        R<?> result = new R<>();
        result.setMessage(ResponseMessageUtil.getMessage(responseMessageEnum.getMessageKey()));
        result.setCode(responseMessageEnum.getMessageKey());
        result.setStatus(1);
        return result;
    }

    public static R<?> fail(ResponseMessageEnum messageEnum, String message) {
        R<?> result = new R<>();
        result.setMessage(message);
        result.setCode(messageEnum.getMessageKey());
        result.setStatus(1);
        return result;
    }

    public static R<?> fail(String code, String message) {
        R<?> result = new R<>();
        result.setCode(code);
        result.setMessage(message);
        result.setStatus(1);
        return result;
    }
    public static R<?> error(String message) {
        R<?> result = new R<>();
        result.setMessage(message);
        result.setCode(ResponseMessageEnum.SYSTEM_ERROR.getMessageKey());
        result.setStatus(1);
        return result;
    }

    public static <T> R<T> fail(T modelData,ResponseMessageEnum responseMessageEnum) {
        R<T> result = new R<T>();
        result.setMessage(ResponseMessageUtil.getMessage(responseMessageEnum.getMessageKey()));
        result.setCode(responseMessageEnum.getMessageKey());
        result.setStatus(1);
        result.setData(modelData);
        return result;
    }

    public static <T> R<T> ok(T modelData,ResponseMessageEnum responseMessageEnum) {
        R<T> result = new R<T>();
        result.setMessage(ResponseMessageUtil.getMessage(responseMessageEnum.getMessageKey()));
        result.setCode(responseMessageEnum.getMessageKey());
        result.setStatus(0);
        result.setData(modelData);
        return result;
    }
}
